import json

from django.shortcuts import render

from rest_framework.response import  Response
from rest_framework.views import APIView
from user import models,user_ser

from rest_framework.generics import ListAPIView,CreateAPIView
from utils import send,myredis,jwt_tool
from rest_framework.pagination import PageNumberPagination

# Create your views here.
from django.core.files.storage import default_storage
class Uploads(APIView):
    def post(self,request):
        file=request.data.get('file')
        types=['png','jpg']
        if file.name.split('.')[-1] not in types:
            return Response({"code":400,'msg':'文件类型错误'})
        if file.size>1024*1024*4:
            return Response({"code":400,'msg':'文件过大'})

        uploadsfile=default_storage.save(file.name,file)
        url=default_storage.url(uploadsfile)
        return Response({"url":url})

import re,random
class Code(APIView):
    def get(self,request):
        phone=request.query_params.get('phone')
        print('-------------',phone)
        re_phone=re.match(r'^1[3-9]\d{9}$',phone)
        if not re_phone:
            return Response({'code':400,'msg':'手机号不符合规则'})
        code=random.randint(1000,9999)
        sendcode=send.send_message(phone,code)
        result=json.loads(sendcode)
        if result['statusCode']=='000000':
            print(result)
            # 存储倒redis中
            myredis.my_redis.setcode(phone,code)

            return Response({'code': 200, 'msg': '发送成功',})
        else:
            return Response({'code': 400, 'msg': '发送失败',})


class Login(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        password=request.data.get('password')

        user=models.UserModel.objects.filter(phone=phone).first()
        if not user:
            return Response({'code': 400, 'msg': '用户名不存在'})
        if password==user.password:
            token = jwt_tool.tool.get_token(user.id)
            return Response({'code': 200, 'msg': '登录成功', 'data': {"token": token, 'userid': user.id}})
        else:
            return Response({'code': 200, 'msg': '密码错误'})


class LoginCode(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        code=request.data.get('code')

        re_phone=re.match(r'^1[3-9]\d{9}$',phone)
        if not re_phone:
            return Response({'code':400,'msg':'手机号不符合规则'})
        rediscode=myredis.my_redis.getcode(phone)
        if rediscode==code:
            user=models.UserModel.objects.filter(phone=phone).first()
            if user:
                #留给token 的位置
                token=jwt_tool.tool.get_token(user.id)
                return Response({'code':200,'msg':'登录成功','data':{"token":token,'userid':user.id}})
            else:
                user=models.UserModel.objects.create(phone=phone,password='123456')
                user.save()
                return Response({'code': 200, 'msg': '没有账号完成注册'})

class Add(CreateAPIView):
    queryset = models.Commtend.objects.all()
    serializer_class = user_ser.Commtend

class MyPage(PageNumberPagination):
    page_size_query_param = 'size'

class Show(ListAPIView):
    queryset = models.Commtend.objects.all()
    serializer_class = user_ser.Commtend
    pagination_class = MyPage



